package com.restaurant.springboot.service.impl;

import com.restaurant.springboot.entity.DishesEntity;
import com.restaurant.springboot.entity.DishesSeriesEntity;
import com.restaurant.springboot.entity.OrderEntity;
import com.restaurant.springboot.entity.OrderItemEntity;
import com.restaurant.springboot.entity.vo.OrderItemVO;
import com.restaurant.springboot.repositories.DishesRepository;
import com.restaurant.springboot.repositories.DishesSeriesRepository;
import com.restaurant.springboot.repositories.OrderItemRepository;
import com.restaurant.springboot.repositories.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class DishesServiceImpl implements com.restaurant.springboot.service.DishesService {

    @Autowired
    private DishesRepository dishesRepository;

   @Autowired
    OrderRepository orderRepository;


    @Autowired
    OrderItemRepository orderItemRepository;

    @Override
    public Page<DishesEntity> getDishesOfPage(Pageable pageable){
        return dishesRepository.findAll(pageable);
    }

    @Override
    public List<DishesEntity> getAllDishes(){return dishesRepository.findAll();}

    @Override
    @Transient
    public boolean saveDishes(DishesEntity dishes){
        if(dishesRepository.save(dishes)!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<DishesEntity> getDishesBySeriesId(int dishesSeriesId){
        return dishesRepository.findAllByDishesSeriesId(dishesSeriesId);
    }

    @Override
    public DishesEntity getDishesById(int dishesId){
        return dishesRepository.findById(dishesId).get();
    }

    @Override
    @Transient
    public void deleteDishesByDishesId(int dishesId){
        dishesRepository.deleteById(dishesId);
    }

    @Override
    public List<DishesEntity> getAllDishesByOrderId(int orderId) {
        Optional<OrderEntity> result = orderRepository.findById(orderId);
        if(result.isPresent()){
            OrderEntity orderEntity = result.get();
            List<DishesEntity> dishesEntities = new ArrayList<>();
            List<OrderItemEntity> allByOrOrderId = orderItemRepository.findAllByOrOrderId(orderEntity.getId());
            allByOrOrderId.forEach((e)->{
                Optional<DishesEntity> dishResult = dishesRepository.findById(e.getDishesId());
                dishResult.ifPresent((x)->{
                    dishesEntities.add(x);
                });
            });
            return dishesEntities;
        }else{
            return null;
        }
    }

    @Override
    public List<OrderItemVO> getAllOrderItemByOrderId(int orderId) {
        Optional<OrderEntity> result = orderRepository.findById(orderId);
        if(result.isPresent()){
            OrderEntity orderEntity = result.get();
            List<OrderItemVO> dishesEntities = new ArrayList<>();
            List<OrderItemEntity> allByOrOrderId = orderItemRepository.findAllByOrOrderId(orderEntity.getId());
            allByOrOrderId.forEach((e)->{
                Optional<DishesEntity> dishResult = dishesRepository.findById(e.getDishesId());
                dishResult.ifPresent((x)->{
                    OrderItemVO orderItemVO = new OrderItemVO();
                    orderItemVO.setName(x.getName());
                    orderItemVO.setNumber(e.getNumber());
                    orderItemVO.setUrl(x.getPhotoUrl());
                    orderItemVO.setPrices(e.getPrice());
                    dishesEntities.add(orderItemVO);
                });
            });
            return dishesEntities;
        }else{
            return null;
        }
    }

    @Override
    public List<DishesEntity> getRecommendDishes() {
        List<DishesEntity> allByState = dishesRepository.findAllByState(1);
        return allByState;
    }

}
